package qlsl.androiddesign.http.service.subservice;

import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.SoftwareConstant;
import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.util.commonutil.PagerUtils;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 订单模块 <br/>
 * 
 */
public class OrderService extends BaseService {

	private static String className = getClassName(OrderService.class);

	/**
	 * 查询订单列表<br/>
	 * 
	 */
	public static void queryOrderList(final int page, final FunctionView<?> functionView, final HttpListener listener) {
		functionView.setProgressBarText("正在查询订单");
		final HttpHandler handler = getHandler(functionView, listener, className, "queryOrderList");
		new HandlerThread(className, "queryOrderList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getorderlistbyself").addParam("pageindex", page)
							.addParam("pagesize", SoftwareConstant.PAGER_SIZE).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					HashMap<String, Object> map = new HashMap<String, Object>();
					List<ShoppingDetail> list = JSONArray.parseArray(jo.getString("rows"), ShoppingDetail.class);
					List<ShoppingDetail> list_sell = JSONArray.parseArray(jo.getString("rows2"), ShoppingDetail.class);
					Pager pager = PagerUtils.createPager(page, jo);
					map.put("list", list);
					map.put("list_sell", list_sell);
					map.put("pager", pager);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, map);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}
}
